<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form" auto-complete="on" label-position="left">

      <div class="login-bg">
        <div class="title-container">
          <h3 class="title">
            <img src="../../assets/common/logo.png" alt="">
          </h3>
        </div>

        <el-form-item style="width:100%" prop="user">
          <span class="svg-container  el-icon-mobile" />
          <el-input v-model.trim="loginForm.user" />
        </el-form-item>

        <el-form-item style="width:100%" prop="password">
          <span class="svg-container  el-icon-lock " />
          <el-input ref="pwd" v-model.trim="loginForm.password" :type="passwordType" />
          <span class="svg-container pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open' " />
          </span>
        </el-form-item>
        <el-form-item style="width:100%" prop="code" class="code">
          <span class="svg-container  el-icon-set-up" />
          <el-input v-model.trim="loginForm.code" placeholder="请输入验证码" />
          <img class="codeImg" :src="imgUrl" alt="" @click="imgCodeFn">
        </el-form-item>

        <el-button type="primary" class="login-btn" :loading="loading" style="width:100%;margin-bottom:30px;" @click="login">登录</el-button>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validUser, validCode } from '@/utils/validate'
import { imgCodeFnAPI, loginAPI } from '@/api/login'
import SvgIcon from '@/components/SvgIcon'
export default {
  name: 'Login',
  components: {
    SvgIcon
  },
  data() {
    const userValid = (rule, value, callback) => {
      // 账号格式验证规则
      if (!validUser(value)) {
        callback(new Error('用户账号格式错误'))
      } else {
        callback()
      }
    }
    const codeValid = (rule, value, callback) => {
      // 验证码格式验证规则
      if (!validCode(value)) {
        callback(new Error('验证码格式错误'))
      } else {
        callback()
      }
    }
    return {
      passwordType: 'password', // 密码是否可见
      loginForm: {
        // 用户登录需要携带的参数
        user: 'admin',
        password: 'admin',
        code: '',
        clientToken: '',
        loginType: 0
      },
      rules: {
        user: [
          { required: true, message: '用户账号必填', trigger: 'blur' },
          { validator: userValid, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { min: 4, max: 16, message: '密码格式错误', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码必填', trigger: 'blur' },
          { validator: codeValid, trigger: 'blur' }
        ]
      },
      loading: false, // 登录按钮加载禁止点击并转圈条件
      imgUrl: ''
    }
  },
  created() {
    this.imgCodeFn() // 页面生成时调用验证码图片生产方法
  },
  methods: {
    showPwd() { // 密码是否可见按钮方法
      this.passwordType === 'password' ? this.passwordType = '' : this.passwordType = 'password'
      this.$nextTick(() => {
        this.$refs.pwd.focus() // 光标聚焦
      })
    },

    open(msg) { // 登陆失败的提示文本
      this.$alert(msg, '提示', {
        confirmButtonText: '确定'
      })
      this.imgCodeFn() // 提示完重新加载验证图片
    },

    async login() { // 用户点击登陆发起登陆请求
      try {
        await this.$refs.loginForm.validate() // 发起登陆请求时发起登陆表单信息验证
        this.loading = true // 登陆按钮转圈打开
        const { data } = await loginAPI({ // 发起登陆验证
          clientToken: this.loginForm.clientToken,
          code: this.loginForm.code,
          loginName: this.loginForm.user,
          loginType: this.loginForm.loginType,
          password: this.loginForm.password
        })
        if (data.success) { // 登陆成功
          this.$store.commit('user/SET_TOKEN', data.token) // 存放用户token
          this.$store.commit('user/SET_LODIN_DATA', data) // 存放用户其他个人信息
          this.$router.push('/home') // 验证成功后跳转至首页
        } else {
          this.open(data.msg) // 登陆失败进行提示，因为什么失败
        }
      } finally {
        this.loading = false // 登陆按钮转圈关闭
      }
    },
    async imgCodeFn() { // 验证码图片请求
      try {
        this.loginForm.clientToken = Math.random() // 生产请求验证码图片需要一个随机数
        const res = await imgCodeFnAPI(this.loginForm.clientToken) // 发起验证码请求
        this.imgUrl = window.URL.createObjectURL(res.data) // 转换验证码地址格式
      } catch (error) {
        console.log(error) // 失败提示---未写
      }
    }
  }

}
</script>

<style lang="scss">

$bg:#fff;
$light_gray:#adadad;
$cursor: #adadad;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url('~@/assets/common/background.png'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .el-input {
    display: inline-block;
    height: 50px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 50px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #e2e2e2;
    background: #fff;
    border-radius: 5px;
    color: #454545;
    .el-form-item__content {
      display: flex;
      align-items: center;
    }
  }
}

.title{
  img {
    width: 96px;
    height: 96px;
  }
}
.login-btn {
  background-color: #586be1;
  height: 52px;
  border-color: #586be1;
}

</style>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 538px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    .login-bg {
      display: flex;
      flex-direction: column;
      justify-content: center;
     align-items: center;
     background-color: #fff;
     padding: 0 30px 0;
     border-radius: 10px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    font-size: 16px;
  }

  .title-container {
    position: relative;
    height: 70px;

    .title {
      position: relative;
      top: -50px;
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .pwd{
    position: absolute;
    height: 100%;
    width: 30px;
    right: 10px;
    font-size: 16px;
    color: #889aa4 !important;
    cursor: pointer;
    // background-color: #777;
  }

}
</style>
